﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using System.Reflection;
using Shpm.System.Web.Control;
using log4net;

public partial class App_Input_PersonInfoFamily : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Context.Items.Contains("PERSON_ID"))
            {
                ViewState["PERSON_ID"] = Context.Items["PERSON_ID"];
            }
            else
            {
                ViewState["PERSON_ID"] = null;
            }
            if (Context.Items["EMPLOYEE_ID"] != null)
            {
                ViewState["EMPLOYEE_ID"] = Context.Items["EMPLOYEE_ID"];
            }
            else
            {
                ViewState["EMPLOYEE_ID"] = null;
            }
            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                ViewState["ParamPageIndex"] = Convert.ToString(Context.Items["ParamPageIndex"]);
            }
            // 接收传递参数为父页编号
            if (Context.Items.Contains("URL"))
            {
                ViewState["URL"] = Convert.ToString(Context.Items["URL"]);
            }
            if (Context.Items.Contains("CMB_GROUP_ID"))
            {
                this.ViewState["CMB_GROUP_ID"] = Context.Items["CMB_GROUP_ID"];
            }
            if (Context.Items.Contains("CMB_JOB_ID"))
            {
                this.ViewState["CMB_JOB_ID"] = Context.Items["CMB_JOB_ID"];
            }
            if (Context.Items.Contains("EMPLOYEE_STATUS"))
            {
                this.ViewState["EMPLOYEE_STATUS"] = Context.Items["EMPLOYEE_STATUS"];
            }
            //ViewState["PERSON_ID"] = 127;
            ViewState["STATE"] = "INS";
            if (Convert.ToString(ViewState["EMPLOYEE_STATUS"]) == "-1")
            {
                btnAdd.Enabled = false;
            }
            else
            {
                btnAdd.Enabled = true;
            }
        }
    }
    #region 设置标签
    protected void lnkTab_Command(object sender, CommandEventArgs e)
    {
        string directUrl = "~/App/Input/SaleProcesseMembershipForm.aspx";
        switch (e.CommandName)
        {
            case "btn0":
                // 基本信息
                // this.LinkButton1.CssClass = "selectli";
                // this.LinkButton2.CssClass = "";
                directUrl = "~/App/Input/PersonInfoForm.aspx";
                break;
            case "btn1":
                // 学历
                directUrl = "~/App/Input/PersonInfoEdu.aspx";
                break;
            case "btn3":
                // 工作经历
                directUrl = "~/App/Input/PersonInfoWork.aspx";
                break;
            case "btn2":
                // 家庭情况
                directUrl = "~/App/Input/PersonInfoFamily.aspx";
                break;
            case "btn4":
                // 福利
                directUrl = "~/App/Input/PersonInfoWelfare.aspx";
                break;
            case "btn5":
                //薪酬
                directUrl = "~/App/Input/PersonInfoSalaryForm.aspx";
                break;
            case "btn6":
                //奖惩
                directUrl = "~/App/Input/PersonBonusPenaltyForm.aspx";
                break;
            case "btn7":
                //报道
                directUrl = "~/App/Input/PersonPrepareForm.aspx";
                break;
            case "btn8":
                //培训
                directUrl = "~/App/Input/PersonTrainForm.aspx";
                break;
            default:
                break;
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["PERSON_ID"])))
        {
            Context.Items["PERSON_ID"] = this.ViewState["PERSON_ID"];
            Context.Items["EMPLOYEE_ID"] = this.ViewState["EMPLOYEE_ID"];
            Context.Items["CMB_JOB_ID"] = ViewState["CMB_JOB_ID"];
            Context.Items["CMB_GROUP_ID"] = ViewState["CMB_GROUP_ID"];
            Context.Items["ParamPageIndex"] = ViewState["ParamPageIndex"];
            Context.Items["URL"] = ViewState["URL"];
            Context.Items["EMPLOYEE_STATUS"] = ViewState["EMPLOYEE_STATUS"];
            ControllerUtils.Transfer(this.Context, directUrl, false);
        }
        else
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0035").ToString();
        }

    }
    #endregion
    protected void GridView2_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int row = Convert.ToInt32(e.RowIndex);
        string workId = (this.GridView2.Rows[row].FindControl("rowId") as Label).Text;
        PersonFamilyBean bean = new PersonFamilyBean();
        PersonDao dao = new PersonDao();
        bean.PersonId = Convert.ToInt32(ViewState["PERSON_ID"]);
        bean.RowId = Convert.ToInt32(workId);
        dao.DelFamily(bean);

        ((Design_MasterPage)this.Master).InfoMessage =
                         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();
        InitFamily();
    }
    protected void GridView2_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        int row = Convert.ToInt32(e.NewEditIndex);
        string rowId = (this.GridView2.Rows[row].FindControl("rowId") as Label).Text;
        PersonFamilyBean bean = new PersonFamilyBean();
        PersonDao dao = new PersonDao();
        bean.PersonId = Convert.ToInt32(ViewState["PERSON_ID"]);
        bean.RowId = Convert.ToInt32(rowId);

        bean = dao.GetFamily(bean);

        this.relation.Text = bean.Relation;
        this.name.Text = bean.Name;
        this.age.Text = bean.Age;
        this.company.Text = bean.Company;
        this.job.Text = bean.Job;
        this.workSpan.InnerText = "修改工作经历";
        ViewState["STATE"] = "UPD";
        ViewState["ID"] = rowId;
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号

        GridView2.PageIndex = e.NewPageIndex;
        FindFamilyList(new PersonDao());
    }
    private void FindFamilyList(PersonDao dao)
    {
        IList list = new ArrayList();
        PersonFamilyBean familyBean = new PersonFamilyBean();
        familyBean.PersonId = Convert.ToInt32(ViewState["PERSON_ID"]);
        list = dao.FindFamily(familyBean);

        this.GridView2.DataSource = list;
        this.GridView2.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (this.ComValidator1.DoValidator() == false)
        {

            ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator1.ErrorMessage;

            return;
        }
        PersonDao dao = new PersonDao();
        SaveFamily(dao);
        InitFamily();
    }
    void Page_PreRender(object sender, EventArgs e)
    {
        if (ViewState["PERSON_ID"] != null)
        {
            PersonDao dao = new PersonDao();
            FindFamilyList(dao);
        }

    }

    /*
     * 函数名：InitFamily
     * 功能：数据保存
     * 参数：无
     * 说明：新规
     */
    protected void InitFamily()
    {
        this.relation.Text = "";
        this.name.Text = "";
        this.age.Text = "";
        this.company.Text="";
        this.job.Text="";

        this.workSpan.InnerText = "新增家庭情况";
        ViewState["STATE"] = "INS";
    }

    /*
     * 函数名：SaveFamily
     * 功能：数据保存
     * 参数：无
     * 说明：
     */
    private int SaveFamily(PersonDao dao)
    {
        PersonFamilyBean bean = new PersonFamilyBean();
        bean.Relation = this.relation.Text;
        bean.Company = this.company.Text;
        bean.Name = this.name.Text;
        bean.Age = this.age.Text;
        bean.Job = this.job.Text;
        bean.PersonId = Convert.ToInt32(ViewState["PERSON_ID"]);
        SysUserManager manager = new SysUserManager(Context);
        bean.UserId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
        if ("INS".Equals(Convert.ToString(ViewState["STATE"])))
        {
            dao.InsertFamily(bean);

            /*保存成功*/
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
        }
        else if ("UPD".Equals(Convert.ToString(ViewState["STATE"])))
        {
            // bean.SaleProcId = Convert.ToInt32(ViewState["PROC_ID"]);
            // bean.PersonId = Convert.ToInt32(ViewState["personId"]);
            // bean.CurrentStep = (Convert.ToInt32(ViewState["CUR_STEP"])) >= 1 ? (Convert.ToInt32(ViewState["CUR_STEP"])) : 1;
            // dao.UpdInfo(bean);
            bean.RowId = Convert.ToInt32(ViewState["ID"]);
            dao.UpdFamily(bean);
            /*保存成功*/
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
        }
        return 0;
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        // 传递分页编号
        Context.Items["ParamPageIndex"] = ViewState["ParamPageIndex"];
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["CMB_GROUP_ID"]);
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["CMB_JOB_ID"]);
        //string url = Convert.ToString(ViewState["URL"]);
        string url = "~/App/Input/PersonEmployeeSrhForm.aspx";
        ControllerUtils.Transfer(Context, url, true);    
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (Convert.ToString(ViewState["EMPLOYEE_STATUS"]) == "-1")
            {
                (e.Row.FindControl("ImageButton1") as ImageButton).Enabled = false;
            }
        }
    }
}
